package cn.openea.perpartner.cms.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.cms.web.entity.CmsSales;
import cn.openea.perpartner.cms.web.entity.CmsSalesAssistant;
import cn.openea.perpartner.cms.web.entity.SysRole;
import cn.openea.perpartner.cms.web.entity.SysUserRole;
import cn.openea.perpartner.cms.web.mapper.CmsSalesAssistantMapper;
import cn.openea.perpartner.cms.web.mapper.SysRoleMapper;
import cn.openea.perpartner.cms.web.mapper.SysUserRoleMapper;
import cn.openea.perpartner.cms.web.service.ICmsSalesAssistantService;
import cn.openea.perpartner.cms.web.service.ICmsSalesService;
import cn.openea.perpartner.system.util.ToolsUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 销售助理列表(CmsSalesAssistant)表服务实现类
 *
 * @author Weird077
 * @since 2022-09-04 08:51:36
 */
@Service("salesAssistantService")
public class CmsSalesAssistantServiceImpl extends ServiceImpl<CmsSalesAssistantMapper, CmsSalesAssistant> implements ICmsSalesAssistantService {
    @Autowired
    private ICmsSalesService salesService;

    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public List<CmsSalesAssistant> selectAll(String userId, String searchStr) {
        LambdaQueryWrapper<CmsSalesAssistant> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(userId)) {
            wrapper.eq(CmsSalesAssistant::getCreateUser, userId);
        }
        if (StrUtil.isNotBlank(searchStr)) {
            wrapper.and(wrap -> wrap.like(CmsSalesAssistant::getOpeneaId, searchStr)
                    .or().like(CmsSalesAssistant::getUserName, searchStr)
                    .or().like(CmsSalesAssistant::getPost, searchStr)
                    .or().like(CmsSalesAssistant::getEducation, searchStr)
                    .or().like(CmsSalesAssistant::getPhone, searchStr)
                    .or().like(CmsSalesAssistant::getEntryTime, searchStr)
                    .or().like(CmsSalesAssistant::getAge, searchStr));
        }
        wrapper.orderByDesc(CmsSalesAssistant::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<CmsSales> selectSalesList(String userId, String searchStr) {
        LambdaQueryWrapper<CmsSales> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsSales::getAssistantId, userId);
        if (StrUtil.isNotBlank(searchStr)) {
            wrapper.and(wrap -> wrap.like(CmsSales::getOpeneaId, searchStr)
                    .or().like(CmsSales::getUserName, searchStr)
                    .or().like(CmsSales::getPost, searchStr)
                    .or().like(CmsSales::getEducation, searchStr)
                    .or().like(CmsSales::getPhone, searchStr)
                    .or().like(CmsSales::getEntryTime, searchStr)
                    .or().like(CmsSales::getAge, searchStr));
        }
        return salesService.list(wrapper);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean insert(CmsSalesAssistant salesAssistant) {
        //1、生成平台id,并补充创建者信息
        salesAssistant.setCreateUser(UserContext.getUserId());
        salesAssistant.setUpdateUser(UserContext.getUserId());
        String openeaId = salesAssistant.getOpeneaId();
        if (StrUtil.isBlank(openeaId)) {
            openeaId = ToolsUtil.autoUserID();
            salesAssistant.setOpeneaId(openeaId);
        }
        //2、将销售助理加入销售助理角色成员
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleKey, "salesManage");
        Long roleId = roleMapper.selectOne(wrapper).getRoleId();
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(openeaId);
        userRoleMapper.insert(userRole);
        return save(salesAssistant);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean removeBatch(List<Long> idList) {
        //1、获取销售助理角色id
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleKey, "salesManage");
        Long roleId = roleMapper.selectOne(wrapper).getRoleId();
        //2、查询删除的销售助理列表，获取销售助理 用户编号集合
        LambdaQueryWrapper<CmsSalesAssistant> wrap = new LambdaQueryWrapper<>();
        wrap.in(CmsSalesAssistant::getSalesId, idList);
        List<CmsSalesAssistant> assistantList = list(wrap);
        List<String> userIdList = new ArrayList<>();
        assistantList.forEach(assistant -> userIdList.add(assistant.getOpeneaId()));
        //3、从销售助理角色成员中移除 销售助理用户编号
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getRoleId, roleId);
        queryWrapper.in(SysUserRole::getUserId, userIdList);
        userRoleMapper.delete(queryWrapper);
        //4、更新销售的当前助理
        LambdaQueryWrapper<CmsSales> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.in(CmsSales::getAssistantId, userIdList);
        List<CmsSales> salesList = salesService.list(queryWrap);
        List<CmsSales> updateCmsSalesList = new ArrayList<>();
        salesList.forEach(sales -> {
            sales.setAssistantId("");
            sales.setAssistantName("");
            updateCmsSalesList.add(sales);
        });
        salesService.updateBatchById(updateCmsSalesList);
        //5、删除销售助理
        return removeByIds(idList);
    }
}

